<template>
	<div class="container">
		<div class="login-content">
			<img v-if="logoFlag"src="assistant/assets/images/logoA.png">
			<img :src="logoUrl" v-if="!logoFlag">
		</div>
		<div class="sign-list">
			<ul class="login-list">
				<li>
					<i class="fa fa-user"></i>
					<input type="text" placeholder="请输入用户名" v-model="loginData.phone" v-on:blur="getCompanyList"/>
				</li>
				<li>
					<i class="fa fa-lock"></i>
					<input type="password" placeholder="请输入密码" v-model="loginData.password" />
				</li>
				<li>
					<i class="fa fa-bank"></i>
					<select v-model="loginData.companyUuid" @change="setCompanyUuid">
						<option value="">请选择开发商</option>
						<option v-for="company in companyList" v-bind:value="company.companyUuid">{{company.companyName}}</option>
					</select>
				</li>
				<a href = "javascript:void(0)" @click="toForgetPassword"style="color:#666;float: right;text-decoration: underline;">忘记密码</a>
			</ul>
			<mt-button size="large" type="danger" @click.native="handleClick">登录</mt-button>
		</div>
	</div>
</template>
<script>
	import { Toast } from 'mint-ui'
	export default {
		data(){
			return {
				logoUrl:'',
				logoFlag:true,
				loginData:{
					 phone:'',
					 password:'',
					 wxOpenId: window.sessionStorage.getItem(this.utilHelper.appAssistant+"_wxOpenid"),
					 domain:'',
					 companyUuid:''
				},
                companyList: []
			}
		},
		mounted: function() {
			//this.getCompanyLogo();
		},
		methods :{
			toForgetPassword(){
				this.$router.push({
					path:'/assistantForgetPassword'
				});
			},
			getCompanyLogo(){
				var _self=this;
				var body = {};
				body.params = {'logoType':_self.utilHelper.appAssistant}
				var url = _self.utilHelper.apiUrl + "/api/getLogo";
				_self.$http.post(url,body).then((response) =>{
					var data = response.body.resData;
					if(data.result=='1'){
						_self.logoFlag=false;
						_self.logoUrl = data.logoUrl;
					}
				})
			},
			handleClick() {
				var _self=this;
				if(_self.vaild()){
					var body = {};
					body.params = _self.loginData;
		  			var url = _self.utilHelper.apiUrl + "/api/login";
			   	 	_self.$http.post(url, body).then((response) => {
						var data = response.body.resData;
                        var result = data.result;
                        var errorMsg = data.resultMsg;
                        const appAssistant = _self.utilHelper.appAssistant;
	    		    	if(result=="1"){
	    		    		window.sessionStorage.setItem(appAssistant+"_userId", data.id);
	    		    		window.sessionStorage.setItem(appAssistant+"_userName", data.accountName);
	    		    		window.sessionStorage.setItem(appAssistant+"_phone", data.phone);
	    		    		window.sessionStorage.setItem(appAssistant+"_premisesId", data.premisesId);
					        window.sessionStorage.setItem(appAssistant+"_premisesName", data.premisesName);
					        window.sessionStorage.setItem(appAssistant+"_isLogin", true);
                  			window.sessionStorage.setItem("view","assistantLogin");
                  			window.sessionStorage.setItem("companyUuid",_self.loginData.companyUuid);
					        _self.$router.replace({
						     	path: '/assistant'
					        })
	    		    	}else{
	    		    		if(errorMsg){
	    		    			Toast({
								  	message: errorMsg,
								  	position: 'middle',
								  	duration: 3000
								});
	    		    		}
	    		    	}
			  		}, (response) => {
			  			Toast({
						  message: '登陆超时',
						  position: 'middle',
						  duration: 3000
						});
				  	});
				}
			},
			vaild () {;
				var _self = this;
				if(!_self.loginData.phone){
					Toast({
					  message: '请输入用户名',
					  position: 'middle',
					  duration: 3000
					});
					return false;
				}
				if(!_self.loginData.password){
					Toast({
					  message: '请输入密码',
					  position: 'middle',
					  duration: 3000
					});
					return false;
				}
                /*if (_self.companyList.length == 0) {
                    layer.msg("用户名或密码错误", {
                        time: 2000
                    });
                    return false;
                }*/
                if (!_self.loginData.companyUuid) {
                    Toast({
                        message: '请选择开发商',
                        position: 'middle',
                        duration: 3000
                    });
                    return false;
                }
				return true;
			},
            getCompanyList(){
                var _self = this;
                var body = {};
                if (this.loginData.phone) {
                    var params = {
                        phone : this.loginData.phone
                    };
                    body.params = params;
                    var url = _self.utilHelper.apiUrl + "/api/getCompanyByAssistantPhone";
                    _self.$http.post(url, body).then((response) => {
                        var data = response.body.resData;
                        var result = data.result;
                        var errorMsg = data.resultMsg;
                        _self.loginData.domain='';
                        if (result == "1") {
                            _self.companyList = data.companyList;
                            if (data.companyList.length == 1) {
                                _self.loginData.domain = data.companyList[0].domain;
                            }
                        }
                    }, (response) => {

                    });
                }
            },
            setCompanyUuid(){
                var _self = this;
                if(_self.loginData.companyUuid){
                    window.sessionStorage.setItem("companyUuid",_self.loginData.companyUuid);
                }
			}
		}
	}
</script>
<style scoped>
	.login-content img{height: 260px;width: 100%;}
	.login-list{margin: 50px 0;}
	.sign-list{padding: 0 22px;}
	.login-list li{border-bottom: 1px solid #cdcdcd;line-height: 48px;height: 48px;padding-left:50px;position: relative;margin-bottom: 10px;padding-right: 20px;}
	.login-list>li .fa{position: absolute;left: 12px;top: 14px;font-size: 20px;color: #cdcdcd;}
	.login-list>li input{height: 24px;line-height: 24px;width: 100%;background-color: #f8f5f3;}
	.login-list>li select{height: 24px;line-height: 24px;width: 100%;background-color: #f8f5f3;}
	.mint-button--danger{background-color: #eb6100;}
</style>